//
//  B_ApplyBusinessVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/19.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_ApplyBusinessVC.h"
#import "B_SubmitFooterView.h"
#import "B_RealNameHeaderView.h"
#import "B_RealNameHeaderTwoView.h"
#import "B_RealNameTVC.h"
#import "B_AddBankModel.h"
#import "B_RealNameFooterView.h"
#import "B_ApplyFooterView.h"
#import "XGPhotoManager.h"

@interface B_ApplyBusinessVC ()<UITextFieldDelegate>
@property (nonatomic, strong) BaseTableView * mainView;

@property (nonatomic, strong) B_SubmitFooterView * submitView;

@property (nonatomic, strong) XGPhotoManager * photoManager;

@property (nonatomic, strong) UIImage * image;
@end

@implementation B_ApplyBusinessVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view = self.mainView;
    B_AddBankModel * model0 = [B_AddBankModel initB_AddBankModelWithTitle:@"" placeholder:@"请填写负责人姓名" mapword:@"姓名" alert:@"请填写负责人姓名"];
    B_AddBankModel * model1 = [B_AddBankModel initB_AddBankModelWithTitle:@"" placeholder:@"请填写负责人电话" mapword:@"电话" alert:@"请填写负责人电话"];
    self.dataArray = [NSMutableArray arrayWithObjects:model0, model1, nil];
    // Do any additional setup after loading the view.
}
- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView addSubview:self.submitView];
        CGFloat bottom = SCREEN_HEIGHT > 800 ? (49 + 24) : 49;
        [self.submitView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mainView.mas_left);
            make.right.equalTo(self.mainView.mas_right);
            make.bottom.equalTo(self.mainView.mas_bottom);
            make.height.equalTo(@(bottom));
        }];
        
        [_mainView setTableViewFrameWithTop:-getNavHeight() left:0 right:0 bottom:-bottom];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_RealNameTVC" bundle:nil] forCellReuseIdentifier:@"B_RealNameTVC"];
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 2;
        };
        
        _mainView.heightForHeaderBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 450;
            } else {
                return 70;
            }
        };
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                B_RealNameHeaderView * view = [B_RealNameHeaderView initB_RealNameHeaderView];
                view.B_applyBussinessView.hidden = NO;
                view.B_titleLabel.text = @"申请商家";
                view.B_descriptionLabel.text = @"申请商家成功后, 即可享受免费发布活动、销售商品等特权!";
                view.B_twoTitleLabel.text = @"加入我们的优势";
                return view;
            } else {
                B_RealNameHeaderTwoView * view = [B_RealNameHeaderTwoView initB_RealNameHeaderTwoView];
                view.B_titleLabel.text = @"填写认证信息";
                return view;
            }
        };
        
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 0;
            } else {
                return weSelf.dataArray.count;
            }
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 54;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_AddBankModel * model0 = weSelf.dataArray[indexPath.row];
            B_RealNameTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_RealNameTVC" forIndexPath:indexPath];
            cell.B_realTextfield.placeholder = model0.placeholder_name;
            cell.B_realTextfield.text = model0.value_name;
            cell.B_realTextfield.delegate = weSelf;
            cell.B_realTextfield.tag = indexPath.row;
            return cell;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            
        };
        
        _mainView.heightForFooterBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 0.1f;
            } else {
                return 200;
            }
        };
        _mainView.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return nil;
            } else {
                B_ApplyFooterView * footer = [B_ApplyFooterView initB_ApplyFooterView];
                footer.B_imageView.image = weSelf.image;
                [footer.B_imageView addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
                    [weSelf alertViewWithTitle:@"请选择" message:@"" buttonsTitles:@[@"相机", @"从相册选择", @"取消"] action:^(NSInteger index) {
                        if (index == 0) {
                            [weSelf.photoManager openCamereWithController:weSelf withImage:^(UIImage *image) {
                                weSelf.image = image;
                                footer.B_imageView.image = image;
                            }];
                        } else if (index == 1) {
                            [weSelf.photoManager openSinglePhotoWithController:weSelf withImage:^(UIImage *image) {
                                weSelf.image = image;
                                footer.B_imageView.image = image;
                            }];
                        }
                    }];
                }];
                return footer;
            }
        };
        
    }
    return _mainView;
}

- (B_SubmitFooterView *)submitView {
    if (!_submitView) {
        WeakSelf(weSelf, self);
        _submitView = [B_SubmitFooterView initB_SubmitFooterView];
        [_submitView.B_SubmitButton setTitle:@"提交" forState:UIControlStateNormal];
        _submitView.submitAction = ^{
            //TODO:执行提交操作
            
            [weSelf submitImage];
            
        };
    }
    return _submitView;
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNavigationBarBackgroundWithImage:[UIImage getImageWithColor:[UIColor clearColor] alp:0]];
    [self setNavigationBarShadowImge:[UIImage getImageWithColor:[UIColor clearColor] alp:0]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (XGPhotoManager *)photoManager {
    if (!_photoManager) {
        _photoManager = [[XGPhotoManager alloc] init];
    }
    return _photoManager;
}

- (void) textFieldDidEndEditing:(UITextField *)textField {
    B_AddBankModel * model0 = self.dataArray[textField.tag];
    model0.value_name = textField.text;
}

/**
 提交图片
 */
- (void) submitImage {
    
    NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];
    for (B_AddBankModel * model in self.dataArray) {
        if (ISNULL(model.value_name)) {
            [KeyWindow makeToast:model.alert_message];
            return;
        }
        if ([model.map_word isEqualToString:@"姓名"]) {
            [dictionary setObject:model.value_name forKey:@"legalPersonName"];
        } else if ([model.map_word isEqualToString:@"电话"]) {
            [dictionary setObject:model.value_name forKey:@"legalPersonPhone"];
        }
        
    }
    if (ISNULL(self.image)) {
        [KeyWindow makeToast:@"请上传营业执照!"];
        return;
    }
    [dictionary setObject:getUserId() forKey:@"userId"];
    WeakSelf(weSelf, self);
    [NetworkingManager UploadmoreTaskWithURL:[NSString stringWithFormat:@"%@/yearnfor/inter/businessAuthentication/businessAuthenticationPicture", ServiceIp] imagesArray:@[self.image] parameterOfimages:@"file" parametersDict:dictionary compressionRatio:0.8 succeedBlock:^(NSDictionary *dict) {
        if ([dict[@"code"] isEqualToString:@"20005"]) {
            [KeyWindow makeToast:@"信息提交成功"];
            [weSelf.navigationController popViewControllerAnimated:YES];
        } else {
            [KeyWindow makeToast:dict[@"message"]];
        }
    } failedBlock:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
